package com.sky.controller.admin;

import com.sky.constant.CacheKeyRuleConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController("adminSetmealController")
@Slf4j
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐管理接口")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = CacheKeyRuleConstant.SET_MEAL_KEY, key = "#setmealDTO.getCategoryId()")
    @PostMapping
    public Result save(@RequestBody SetmealDTO setmealDTO) {
        log.info("新增套餐 : {}", setmealDTO);
        setmealService.save(setmealDTO);
        return Result.success();
    }

    /**
     * 条件分页查询
     * @param pageQueryDTO
     * @return
     */
    @ApiOperation("条件分页查询")
    @GetMapping("/page")
    public Result<PageResult> pageQuery(SetmealPageQueryDTO pageQueryDTO) {
        log.info("条件分页查询 : {}", pageQueryDTO);
        PageResult pageResult = setmealService.pageQuery(pageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据 id 查询套餐信息 - 页面回显
     * @param id
     * @return
     */
    @ApiOperation("根据 id 查询套餐信息")
    @GetMapping("/{id}")
    public Result<SetmealVO> getInfoById(@PathVariable Long id) {
        log.info("根据 id 查询套餐信息, {}", id);
        SetmealVO setmealVO = setmealService.getInfoById(id);
        return Result.success(setmealVO);
    }

    /**
     * 套餐信息编辑
     * @param setmealDTO
     * @return
     */
    @ApiOperation("套餐信息编辑")
    @CacheEvict(cacheNames = CacheKeyRuleConstant.SET_MEAL_KEY, allEntries = true)
    @PutMapping
    public Result editInfo(@RequestBody SetmealDTO setmealDTO) {
        log.info("套餐信息编辑:{}", setmealDTO);
        setmealService.editInfo(setmealDTO);
        return Result.success();
    }

    /**
     * 批量删除套餐
     * @param ids 套餐 id 列表
     * @return
     */
    @ApiOperation("批量删除套餐")
    @CacheEvict(cacheNames = CacheKeyRuleConstant.SET_MEAL_KEY, allEntries = true)
    @DeleteMapping
    public Result deleteBatch(@RequestParam List<Long> ids) {
        log.info("批量删除套餐 : {}", ids);
        setmealService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 启售|停售套餐
     * @param status 操作状态
     * @param id 套餐 id
     * @return
     */
    @ApiOperation("启售|停售套餐")
    @CacheEvict(cacheNames = CacheKeyRuleConstant.SET_MEAL_KEY, allEntries = true)
    @PostMapping("/status/{status}")
    public Result enableOrDisable(@PathVariable Integer status, Long id) {
        log.info("启售|停售套餐，设置的状态为: {}，id: {}", status == 1 ? "启售" : "停售", id);
        setmealService.enableOrDisable(status, id);
        return Result.success();
    }

}
